package com.xiaomili.halfsugar.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.activity.ListDetailActivity;
import com.xiaomili.halfsugar.adapter.HomeRecyclerViewAdapter;
import com.xiaomili.halfsugar.bean.HomeBean;
import com.xiaomili.halfsugar.utils.OkHttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ViewPagerFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private LinearLayoutManager mManager;
    private HomeRecyclerViewAdapter mAdapter;
    private List<Object> mTopicList;
    private static final String KEY1 = "param1";
    private static final String KEY2 = "param2";
    private static final String TAG = "ViewPagerFragment";
    private int mListIndex;
    private List<Integer> mList;
    private ArrayList<String> mIdList;

    public static ViewPagerFragment newInstance(String url, int position) {

        Bundle args = new Bundle();
        args.putString(KEY1, url);
        args.putInt(KEY2, position);
        ViewPagerFragment fragment = new ViewPagerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_view_pager, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mManager);
        mTopicList = new ArrayList<>();
        mList = new ArrayList<>();
        mIdList = new ArrayList<>();
        initData();
    }

    private void initData() {
        if (getArguments() != null) {
            String url = getArguments().getString(KEY1);
            final int mIndex = getArguments().getInt(KEY2);
            OkHttpUtil.newInstance(getActivity()).getAsyncData(url, new OkHttpUtil.MyCallBack() {
                @Override
                public void onFailure(IOException e) {

                }

                @Override
                public void onResponse(String result) {
                    Gson gson = new Gson();
                    HomeBean homeBean = gson.fromJson(result, HomeBean.class);
                    final List<HomeBean.DataBean.TopicBean> topic = homeBean.data.topic;
                    mTopicList.addAll(topic);//recyclerview数据源
                    // TODO: 2016/7/21 判断添加到哪个页面
//                    int pageSize = homeBean.data.insert_element.size();//插入数据的总页数
//                    for (int i = 0; i < pageSize; i++) {
//                        int page = homeBean.data.insert_element.get(i).index; //数据插入的页面
//                    }
                    if (mIndex == 0) {
                        int size = homeBean.data.insert_element.get(0).element_list.size();//插入数据的个数
                        for (int i = 0; i < size; i++) {
                            mListIndex = homeBean.data.insert_element.get(0).element_list.get(i).index - 1;//插入数据的位置
                            mList.add(mListIndex);////插入数据位置的集合
                            mTopicList.add(mListIndex, homeBean.data.insert_element.get(0).element_list.get(i));//数据源
                        }
                    }

                    mAdapter = new HomeRecyclerViewAdapter(getActivity(), mIndex, mList, mTopicList);
                    mRecyclerView.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                    setListener(topic);
                }
            });
        }
    }

    private void setListener(final List<HomeBean.DataBean.TopicBean> topic) {

        mAdapter.setOnItemClickListener(new HomeRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View itemView) {
                if (itemView.getId() == R.id.insert_item_iv1) {
                    Toast.makeText(getActivity(), "双图类型1", Toast.LENGTH_SHORT).show();
                }
                if (itemView.getId() == R.id.insert_item_iv2) {
                    Toast.makeText(getActivity(), "双图类型2", Toast.LENGTH_SHORT).show();
                }
                if (itemView.getId() == R.id.iv) {
                    Toast.makeText(getActivity(), "大图类型", Toast.LENGTH_SHORT).show();
                }
                if (itemView.getId() == R.id.normal_layout) {
                    Intent intent = new Intent(getActivity(), ListDetailActivity.class);
                    HomeBean.DataBean.TopicBean bean = (HomeBean.DataBean.TopicBean) mTopicList.get(position);
                    int size = topic.size();
                    int index = 0;
                    for (int i = 0; i < topic.size(); i++) {
                        String id = topic.get(i).id;
                        mIdList.add(id);
                        if (topic.get(i) == mTopicList.get(position)) {
                            index = i;
                        }
                    }
                    intent.putExtra("position", index);
                    intent.putExtra("size", size);
                    intent.putStringArrayListExtra("idList", mIdList);
                    startActivity(intent);
                }
            }
        });
    }
}